package com.yanqu.road.dao.impl.activity.douluodalu;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.douluodalu.data.DouLuoDaLuServerUserData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DouLuoDaLuServerUserDataDaoImpl extends TempDataObjectDao<DouLuoDaLuServerUserData> {

    public static String getDouLuoDaLuServerUserDataSql = "select * from `t_u_douluodalu_user_data` where `activityId`=?  and `userId` =?";
    public DouLuoDaLuServerUserData getDouLuoDaLuServerUserData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getDouLuoDaLuServerUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String insertDouLuoDaLuServerUserDataSql = "insert into `t_u_douluodalu_user_data`(activityId,userId,sendTimes,fightTimes,zhuiBuTimes,resetTime,touchMallMap,totalTimes,commonTimes,drawBoxList," +
            "getSharedBox,lastReadReportTime,triedStoneNum,touchMallTimesData) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(DouLuoDaLuServerUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSendTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getFightTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getZhuiBuTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getResetTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getTouchMallMapJsonStr()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTotalTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCommonTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getDrawBoxList())));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isGetSharedBox()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastReadReportTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTriedStoneNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getTouchMallTimesMapJsonStr()));
        if (getDb().executeNoneQuery(insertDouLuoDaLuServerUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateDouLuoDaLuServerUserDataSql = "update `t_u_douluodalu_user_data` set sendTimes=?,fightTimes=?,zhuiBuTimes = ?," +
            "resetTime=?,touchMallMap=?,totalTimes=?,commonTimes=?,drawBoxList=?,getSharedBox=?,lastReadReportTime=?,triedStoneNum=?," +
            "touchMallTimesData=?  where activityId=? and userId=?";
    @Override
    protected boolean updateData(DouLuoDaLuServerUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getSendTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getFightTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getZhuiBuTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getResetTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getTouchMallMapJsonStr()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTotalTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCommonTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getDrawBoxList())));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isGetSharedBox()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastReadReportTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTriedStoneNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getTouchMallTimesMapJsonStr()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateDouLuoDaLuServerUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected DouLuoDaLuServerUserData parseData(ResultSet rs) throws SQLException {
        DouLuoDaLuServerUserData data = new DouLuoDaLuServerUserData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setSendTimes(rs.getInt("sendTimes"));
        data.setFightTimes(rs.getInt("fightTimes"));
        data.setZhuiBuTimes(rs.getInt("zhuiBuTimes"));
        data.setResetTime(rs.getLong("resetTime"));
        data.setTouchMallMap(JSON.parseObject(rs.getString("touchMallMap"), new TypeReference<ConcurrentHashMap<Integer, Integer>>() {}));
        data.setTotalTimes(rs.getInt("totalTimes"));
        data.setCommonTimes(rs.getInt("commonTimes"));
        String str = rs.getString("drawBoxList");
        if(!StringUtils.isNullOrEmpty(str)){
            data.setDrawBoxList(JSON.parseObject(str, new TypeReference<List<Integer>>() {}));
        }
        data.setGetSharedBox(rs.getBoolean("getSharedBox"));
        data.setLastReadReportTime(rs.getLong("lastReadReportTime"));
        data.setTriedStoneNum(rs.getLong("triedStoneNum"));
        String param = rs.getString("touchMallTimesData");
        if(StringUtils.isNullOrEmpty(param)){
            param = "{}";
        }
        data.setTouchMallTimesMap(JSON.parseObject(param, new TypeReference<ConcurrentHashMap<Integer, Integer>>() {}));
        return data;
    }
}
